package com.veo.config;

import com.veo.bean.User;
import com.veo.converter.MyMessageConverter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.convert.converter.Converter;
import org.springframework.format.FormatterRegistry;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.util.StringUtils;
import org.springframework.web.accept.HeaderContentNegotiationStrategy;
import org.springframework.web.accept.ParameterContentNegotiationStrategy;
import org.springframework.web.servlet.config.annotation.ContentNegotiationConfigurer;
import org.springframework.web.servlet.config.annotation.PathMatchConfigurer;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.util.UrlPathHelper;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Configuration(proxyBeanMethods = false)
public class WebMvcConfig {

    @Bean
    public WebMvcConfigurer webMvcConfigurer(){
        return new WebMvcConfigurer() {

            //自定义一个内容协商的一些规则，可以自定义请求头的内容协商，也可以自定义请求参数的内容协商，自定义的内容会覆盖之前的系统默认的规则
            @Override
            public void configureContentNegotiation(ContentNegotiationConfigurer configurer) {
                //自定义请求参数的内容协商
                Map<String, MediaType> mediaTypes= new HashMap<>();
                //请求参数的名称为ko,mediaType类型为application/x-ko,配置请求参数的内容协商后，默认的请求头的参数会失效，要加入该策略
                mediaTypes.put("ko",MediaType.parseMediaType("application/x-ko"));
                ParameterContentNegotiationStrategy parameterContent = new ParameterContentNegotiationStrategy(mediaTypes);

                //要重新配置
                HeaderContentNegotiationStrategy headerContent = new HeaderContentNegotiationStrategy();

                configurer.strategies(Arrays.asList(parameterContent,headerContent));
            }

            //自定义请求空有关的内容的协商
            @Override
            public void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
                converters.add(new MyMessageConverter());
            }

            /*配置矩阵变量开启*/
            @Override
            public void configurePathMatch(PathMatchConfigurer configurer) {
                UrlPathHelper urlPathHelper = new UrlPathHelper();
                //设置url默认不移除分号后面的东西
                urlPathHelper.setRemoveSemicolonContent(false);
                configurer.setUrlPathHelper(urlPathHelper);
            }

            /*自定义转换器*/
            @Override
            public void addFormatters(FormatterRegistry registry) {
                registry.addConverter(new Converter<String, User>() {

//                    url: /user/张三,22
                    User user = new User();
                    @Override
                    public User convert(String source) {
                        if (StringUtils.hasLength(source)){
                            user.setName(source.split(",")[0]);
                            user.setAge(Integer.parseInt(source.split(",")[1]));
                        }
                        return user;
                    }
                });
            }
        };
    }
}
